package com.flot.yazb.model.user;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flot.yazb.R;
import com.flot.yazb.base.BActivity;
import com.flot.yazb.base.BApplication;
import com.flot.yazb.base.adapter.recycleview.BRAdapter;
import com.flot.yazb.base.adapter.recycleview.RvHolder;
import com.flot.yazb.base.model.BModel;
import com.flot.yazb.aop.stub.CodeStub;
import com.flot.yazb.widget.utils.SharedPreferencesUtils;
import com.flot.yazb.widget.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的特征界面
 * Created by fengdongfei on 2017/9/4.
 */
public class CharacterActivity extends BActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tltit)
    RelativeLayout tltit;
    @BindView(R.id.mrv)
    RecyclerView mrv;
    @BindView(R.id.btsave)
    Button btsave;
    private UserModel model = BApplication.getInstance().getUserModel();
    private List<CharacterModel> dataList = new ArrayList<>();
    private BRAdapter<CharacterModel> rvAdapter;
    private String[] cgaractArray=new String[]{"宅","感性","体贴","憨厚","稳重","好强","冷静","温柔","闷骚"
            ,"自我","幽默","正直","义气","孝顺","勇敢","好动","随和","孤傲"};
    private int maxSelected=5;//最多选择
    private int st=0;
    private String[] args;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_charact);
        ButterKnife.bind(this);
        initAdapter();
        getData();
    }

    private void getSPData() {
        String sps= SharedPreferencesUtils.getString(activity,"character","-1,");
        args = sps.split(",");
        int ss=dataList.size();
        if (args.length>0){
            for (int k=0;k<args.length;k++){
                if (!"-1".equals(args[k])){
                    dataList.get(Integer.parseInt(args[k])).isSelected=true;
                }
            }
            rvAdapter.notifyDataSetChanged();
        }
    }
    private void getData() {
        showProgress();
        CharacterModel.geCharacter(model.id, new BModel.Transit<CharacterModel[]>(this) {
            @Override
            public void onBorn(CharacterModel[] serializable, String code, String message) {
                dismissProgress();
                if (code.equals(CodeStub.B_OK)) {
                    dataList.clear();
                    for (int z=0;z<serializable.length;z++){
                        serializable[z].isSelected=true;
                        dataList.add(serializable[z]);
                    }

                    for (int k=0;k<cgaractArray.length;k++){
                        CharacterModel c=  new CharacterModel();
                        c.tezheng =cgaractArray[k];
                        dataList.add(c);
                    }

                    getSPData();

                    rvAdapter.notifyDataSetChanged();
                } else {
                    ToastUtil.showToast(activity, message);
                }
            }
        });
    }

    private void initAdapter() {
        mrv.setLayoutManager(new GridLayoutManager(this, 4));
        rvAdapter = new BRAdapter<CharacterModel>(this, R.layout.activity_item_text, dataList) {
            @Override
            protected void convert(final RvHolder holder, final CharacterModel model, int position) {
                holder.setText(R.id.tvcontent, model.tezheng);
                if (model.isSelected){
                    holder.setBackgroundRes(R.id.tvcontent, R.drawable.solid_roundangle_theme_5);
                    holder.setTextColorRes(R.id.tvcontent,R.color.white);
                }else{
                    holder.setBackgroundRes(R.id.tvcontent, R.drawable.solid_roundangle_white_5);
                    holder.setTextColorRes(R.id.tvcontent,R.color.gray_text);
                }
                holder.setOnClickListener(R.id.tvcontent, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!model.isSelected){
                            if (maxSelected<=st){
                                ToastUtil.showToast(activity,"最多选择"+maxSelected+"个！");
                                return;
                            }else{
                                holder.setBackgroundRes(R.id.tvcontent, R.drawable.solid_roundangle_theme_5);
                                holder.setTextColorRes(R.id.tvcontent,R.color.white);
                                model.isSelected=true;
                                st=st+1;
                            }
                        }else{
                            holder.setBackgroundRes(R.id.tvcontent, R.drawable.solid_roundangle_white_5);
                            holder.setTextColorRes(R.id.tvcontent,R.color.gray_text);
                            model.isSelected=false;
                            st=st-1;
                        }
                    }
                });

            }
        };

        mrv.setAdapter(rvAdapter);
    }
    StringBuffer sps=new StringBuffer("-1");
    @OnClick({R.id.iv_back, R.id.btsave})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btsave:
                showProgress();
                Map<String,String> map = new HashMap<>();
                map.put("mid", model.id);
                int p=1;
                for (int j=0;j<dataList.size();j++){
                    if (dataList.get(j).isSelected){
                        map.put(p+"", dataList.get(j).tezheng);
                        p=p+1;
                    }
                }
                CharacterModel.setCharacter(map, new BModel.Transit<CharacterModel>(this) {
                    @Override
                    public void onBorn(CharacterModel serializable, String code, String message) {
                        dismissProgress();
                        if (code.equals(CodeStub.B_OK)) {
                            for (int z=0;z<dataList.size();z++){
                                if (dataList.get(z).isSelected==true){
                                    sps.append(","+z);
                                }
                            }
                            Log.d("StringBuffer", "onBorn: "+sps.toString());
                            SharedPreferencesUtils.saveString(activity,"character",sps.toString());
                            finish();
                        } else {
                            ToastUtil.showToast(activity, message);
                        }
                    }
                });
                break;
        }
    }
}
